﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RL
{
    

    /// <summary>
    /// Логика взаимодействия для ucSelectRLByNumber.xaml
    /// </summary>
    /// 
    public partial class ucSelectRLByNumber : UserControl
    {
        private routelistEntities db;



        public int RouteListID
        {
            get { return (int)GetValue(RouteListIDProperty); }
            set { SetValue(RouteListIDProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RouteListID.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RouteListIDProperty =
            DependencyProperty.Register("RouteListID", typeof(int), typeof(ucSelectRLByNumber), new UIPropertyMetadata(0, new PropertyChangedCallback(propID) ));

        public static void propID(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ucSelectRLByNumber item = d as ucSelectRLByNumber;
            DateTime dt;
            if (null == item) return;
            int val = (int)e.NewValue;
            RouteList it = (from o in item.db.RouteList where o.id == val select o).FirstOrDefault();
            if (null == it)
            {
                item.textBox1.Text = "";
                item.datePicker1.SelectedDate = null;
            }
            else
            {
                item.textBox1.Text = it.nomdog;
                if (DateTime.TryParse(it.datedoc, out dt))
                    item.datePicker1.SelectedDate = dt;
            }
        }

        public ucSelectRLByNumber()
        {
            InitializeComponent();
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) return;
            db = new routelistEntities();
            
        }

        private void FindRL()
        {
            int i;
            if (Int32.TryParse(textBox1.Text.Trim(), out i))
            {
                string rslt_num = i.ToString().Trim().PadLeft(8, '0');
                App app = (App)App.Current;
                if (datePicker1.SelectedDate == null) datePicker1.SelectedDate = app.WorkDate;
                string year = datePicker1.SelectedDate.Value.Year.ToString();
                RouteList rl = (from o in db.RouteList where rslt_num == o.nomdog.Trim() && o.datedoc.Substring(6, 4) == year select o).FirstOrDefault();
                if (null == rl)
                {
                    RouteListID = 0;
                    return;
                }
                RouteListID = rl.id;
            }
            else
            {
                RouteListID = 0; // Путевой лист не найден
            }
        }

        private void textBox1_LostFocus(object sender, RoutedEventArgs e)
        {
            FindRL();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
           
        }
    }
}
